package managesystem;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import IO.Hash;

public class changePasswordServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) {
		HttpSession session = req.getSession();
		Student currStud = (Student) session.getAttribute("currStud");
		
		String firstPassword = req.getParameter("firstPassword");
		String secondPassword = req.getParameter("secondPassword");
		
		if(firstPassword.equals(secondPassword)) {
			if(firstPassword.length() > 5) {
				currStud.setPassword(Hash.getHash(firstPassword));
				StudentManager.add(currStud);
				session.setAttribute("currStud", currStud);
				session.setAttribute("message", "Your password has been changed.");
			}
			else {
				session.setAttribute("error", "Your password is not long enough, please try again.");
			}
		}
		else {
			session.setAttribute("error", "These passwords don't match, please try again.");
		}
		try {
			resp.sendRedirect("/index.jsp?id=ucp");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
